<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/crm/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/crm/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/static/crm/css/public.css" media="all">
    <meta name="referrer" content="no-referrer">
</head>

<body>
    <form class="layui-form layui-form-pane layui-container" lay-filter="form" action="">
        <div style="margin: 40px 20px 40px 20px">
            <fieldset class="table-search-fieldset ">
                <div style="margin: 40px 20px 40px 20px">
                    <div class="layui-form-item">
                        <div class="layui-row ">
                            <div class="layui-inline">
                                <label class="layui-form-label">客户:</label>
                                <div class="layui-input-inline">
                                    <select name="client_id" id="client_id" lay-verify="">
                                        <option value=""></option>
                                        {volist name = '$list' id = 'list' }
                                        <option value="{$list.id}">{$list.name}</option>
                                        {/volist}
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">商机名称:</label>
                                <div class="layui-input-inline">
                                    <input id="name" type="text" name="name" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">预算金额:</label>
                                <div class="layui-input-inline">
                                    <input id="money" type="text" name="money" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline"">
                                <label class=" layui-form-label">预计成交时间:</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="predict_time" class="layui-input" id="test" placeholder="">
                                </div>
                            </div>
                            <div class="layui-inline"">
                                <label class=" layui-form-label">下次跟进时间:</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="follow_time" class="layui-input" id="test2" placeholder="">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">备注:</label>
                                <div class="layui-input-inline">
                                    <input id="remark" type="text" name="remark" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </fieldset>
            <fieldset class="table-search-fieldset " style="border: 2px solid rgb(144, 144, 143);">
                <legend style="color:rgb(144, 144, 143);">商品信息</legend>

                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter">

                </table>

                <tr>
                    <td>总价：￥<span id="total">0.00</span> </td>&nbsp;&nbsp;&nbsp;
                    <td>优惠率%: <input type="number" name="" id="discount" max="99" style="width: 40px; "> </td>
                    &nbsp;&nbsp;&nbsp;
                    <td>订单总额：￥ <input type="text" name="subal" id="subal" disabled></td>
                </tr>
                <script type="text/html" id="toolEventDemo">
                            <a class="layui-btn  layui-btn-danger layui-btn-sm " lay-event="del" ><i class="layui-icon layui-icon-delete"></i>删除</a>
                </script>
                <script type="text/html" id="toolbarDemo">
                        <div class="layui-btn-container">
                            <a class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon  layui-icon-edit layui-icon-addition"></i> 添加 </a>
                        </div>
                    </script>

                <!-- </form> -->
            </fieldset>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
        </div>
    </form>
</body>

</html>

<script src="/static/crm/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['laydate', 'jquery', 'form'], function () {
        var laydate = layui.laydate;
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;

        var tableContent = new Array();
        table.render({
            elem: '#currentTableId',
            url: 'lists',
            data: tableContent,
            toolbar: '#toolbarDemo',
            cols: [[
                { field: 'id', width: 60, title: 'ID', sort: true },
                { field: 'name', width: 135, title: '课程名称' },
                { field: 'price', title: '价格', width: 130, sort: true },
                { field: 'aa', width: 60, title: '单位' },
                { field: 'minute_hour', width: 90, title: '课时/分钟' }, // 一对多数据
                { field: 'major', width: 135, templet: function (d) { return d.major.name }, title: '专业' },
                { field: 'campus', width: 135, templet: function (d) { return d.campus.name }, title: '校区' },
                { field: 'campus', width: 215, templet: function (d) { return d.campus.address }, title: '地址' },
                { title: '操作', minWidth: 80, width: 100, toolbar: '#toolEventDemo', align: "center", fixed: 'right' }
            ]],
            done: function (res, curr, count) {
                var dity = 0;
                if (layui.table.getData('currentTableId').length > 0) {
                    for (var i in layui.table.getData('currentTableId')) {
                        dity += parseFloat($(".layui-table-box tbody tr[data-index='" + i + "'] td[data-field='price'] .layui-table-cell").text())
                    }
                }
                $("#subal").val(dity);
                $("#total").text(dity);
                $('#discount').keyup(function () {
                    var discounts = $(this).val();
                    var fina = dity * (discounts * 0.01);
                    $("#subal").val((fina));
                    if ($("#subal").val() == 0.00) {
                        $("#subal").val((dity));
                    }
                })
            }
        });


        //常规用法
        laydate.render({
            elem: '#test'

        });
        laydate.render({
            elem: '#test2'

        });

        var userId = '';
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    type: 2,
                    title: '商品信息',
                    area: ['80%', '80%'],
                    content: 'commodity',
                    success: function (layero, index) {
                        var iframeWindow = window['layui-layer-iframe' + index]  //获取弹框页面
                        iframeWindow.layui.table.on('tool(currentTableFilter)', function (obj) {
                            var data = obj.data;
                            var _id = data['id'];
                            if (obj.event === 'add') {
                                userId += _id + ",";
                                $.get('colis?id=' + userId, function (res) {
                                    if (res.errcode != 1) {
                                        obj.del();
                                        layer.close(index);
                                        table.reload('currentTableId', {
                                            scrollPos: 'fixed'
                                        });
                                    }
                                })
                            }
                        });

                    },
                });
            }
        })

        table.on('tool(currentTableFilter)', function (obj) {
            obj.del();
        });

        /**
         * @Descripttion: 提交
         * @Date: 2022-12-06 10:54:11
         * @param {*} submit
         * @param {*} function
         */
         form.on('submit(formDemo)', function (data) {
            var dity = layui.table.getData('currentTableId');
            function del(obj, keys) {
                keys.map(function (key) {
                    delete obj[key]
                })
                return obj
            }
            var discounts = $('#discount').val()
            var subal = $('#subal').val()

            var arr = Object.values(dity);
           var commodity =[];
            if (arr.length > 0) {
                for (var i in arr) {
                    // arr[i].subtotal = $(".layui-table-box tbody tr[data-index='" + i + "'] td[data-field='subtotal'] .layui-table-cell").text();
                    // arr[i].units = arr[i].units['name'];
                    // arr[i]['shops_id'] = arr[i].id;
                    // arr[i] = del(arr[i], ['create_time', 'delete_time', 'update_time', 'type_id', 'inventory', 'units_id', 'num', 'subtotal', 'attr_id', 'id']);
                    commodity[i]={
                        'course_id':arr[i].id,
                        'price':arr[i].price,
                        'subal':subal,
                        'unit':arr[i].unit,
                        'remark':arr[i].remark,
                        'minute_hour':arr[i].minute_hour,
                        'major_id':arr[i].major_id,
                        'campus_id':arr[i].campus_id,
                    }
                }
            }
            $.ajax({
                url: 'adds',
                type: 'POST',
                dataType: 'JSON',
                data: { 'tract': data.field, 'commodity': commodity },
                success: function (res) {
                    layer.msg(res.msg, {
                        time: 1500,
                        end: function () {
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        }
                    });

                }
            })
            return false;
        });


    });

</script>